//打开二维码扫描窗口
function openQrcod() {
    var FNScanner = api.require('FNScanner');
    FNScanner.open({
        autorotation: true
    }, function (ret, err) {
        if (ret) {
            if (ret.eventType == "success") {
                var content = ret.content;
                if (CheckUrl(content)) {//判断是否是链接
                    var reg = RegExp(/type\/cfnetapp/);
                    if (reg.test(content)) {
                        loadUrlData(content);
                    } else {
                        var reg1 = RegExp(/type\/needuid/);
                        if (reg1.test(content)) {
                            content = content.replace(".html", "");
                            var url = content + '/uid/' + $api.getStorage('cfnetppuid') + '/jid/' + $api.getStorage('cfnetppjxid');
                            openHtml(url);
                            // openHtml(content+'/uid/52804');
                        } else {
                            openHtml(content);
                        }
                    }
                } else {
                    var jsonData = JSON.parse(Base64.decode(content));
                    if (jsonData.type == "sign") {
                        if ($api.getStorage('cfnetppjxid') != jsonData.jid) {
                            api.alert({msg: "请切换集团"});
                            return;
                        }//签到扫描
                        openCourseSignQrode(jsonData);
                    } else if (jsonData.types == "joinplan") {//学习计划
                        joinplan(jsonData);
                    }
                }
            }
        }
    });
}

function loadUrlData(url) {
    cfnetppGET(url, false, function (data, status) {
        if (status == 'success') {
            if (data.code == 200) {
                if (data.type == 2) {
                    var newUrl = data.url + '/uid/' + $api.getStorage('cfnetppuid') + '/jid/' + $api.getStorage('cfnetppjxid');
                    openHtml(newUrl);
                }
            } else {
                api.toast({
                    msg: '加载数据失败'
                });
            }
        } else {
            api.toast({
                msg: '网络异常'
            });
        }
    });
}

function openHtml(url) {
    api.openWin({
        useWKWebView: true,
        name: 'QRCode_Win',
        url: './html/HomePage/QRCode_Win.html',
        pageParam: {
            url: url
        }
    });
}

function CheckUrl(str) {
    var RegUrl = new RegExp();
    RegUrl.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
    if (!RegUrl.test(str)) {
        return false;
    }
    return true;
}

//获取集团名称
function getjidname(jid) {
    var stdydata = $api.getStorage('stdydata');
    //		for (var value of stdydata) {
    //		      if(value.id == jid){
    //		          return value.name
    //		      }
    //		 }
    for (var i = 0; i < stdydata.length; i++) {
        if (stdydata[i].id == jid) {
            return stdydata[i].name
        }
    }

}

//请求参加学习计划接口
function joinplan(list) {

    var jidname = getjidname(list.jid);
    if ($api.getStorage('cfnetppjxid') != list.jid) {
        alert('请先切换至' + jidname + '集团');
        return false;
    }
    var param = {
        uid: $api.getStorage('cfnetppuid'),
        pid: list.pid,
        jid: $api.getStorage('cfnetppjxid'),
        teamid: list.teamid,
    };

    cfnetppPOST(url_joinStudyPlanByQrcode, param, false, function (data, status) {
        if (status == 'success') {
            if (data.code == 200) {
                openLearningPlan(list)
            } else if (data.code == 102) {
                if (isEmpty($api.getStorage('cfnetppjxid'))) {
                    ShowToast('请先加入集团');
                } else {
                    ShowToast('请先切换至' + jidname + '集团');
                }
            } else if (data.code == 103) {
                ShowToast('已加入学习计划');
            } else if (data.code == 104) {
                ShowToast('加入学习计划失败');
            } else {
                ShowToast('加入学习计划失败');
            }
        } else {
            api.toast({
                msg: '网络加载失败',
                duration: 2000,
                location: 'bottom'
            });
        }
    });

}

//打开学习计划列表
function openLearningPlan(list) {

    var user_type = $api.getStorage('cfnetSelectType');
    if (user_type == 0) {//学生身份
        api.openWin({
            useWKWebView: true,
            name: 'Student_studyWindow',
            url: 'html/Study/Student_studyWindow.html',
            pageParam: {
                pid: list.pid,
                sourceurl: window.location.pathname,
                sourcetitle: document.title,
                Type: 0,
            }
        });
    } else {//老师身份
        api.openWin({
            useWKWebView: true,
            name: 'Teacher_NewCourseListWin',
            url: 'html/Teacher_Teach/Teacher_NewCourseListWin.html',
            pageParam: {
                sourceurl: window.location.pathname,
                sourcetitle: document.title,
            }
        });

    }

}

//打开课堂活动签到页面
function openCourseSignQrode(list) {
    api.openWin({
        useWKWebView: true,
        name: 'Student_signedWindow_LuDong_Title',
        url: 'html/Student_Classroom/Student_signedWindow_LuDong_Title.html',
        pageParam: {
            cid: list.cid,
            pid: list.pid,
            ismocc: "index",
            sourceurl: window.location.pathname,
            sourcetitle: document.title,
            qrocde: list.qrcode,
            type: 1,
            iid: list.iid,
            jid: list.jid,
        }
    });

}